<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="股票代码" prop="stockCode">
        <el-input
          v-model="queryParams.stockCode"
          clearable
          placeholder="请输入股票代码"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assetList">
      <el-table-column align="center" label="股票代码" prop="stockCode"/>
      <el-table-column align="center" label="股票名称" prop="stockName"/>
      <el-table-column align="center" label="股票最新价（￥）" prop="stockPrice"/>
      <el-table-column align="center" label="持有数量" prop="number"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['invest:stockApply:edit']"
            type="text"
            @click="handleUpdate(scope.row)"
          >确认
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改人才股票对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body style="margin: 15vh"
               width="800px" @closed="reset">
      <el-descriptions :column="2" border style="margin-bottom: 50px">
        <el-descriptions-item label="股票代码">{{ asset.stockCode }}</el-descriptions-item>
        <el-descriptions-item label="股票名称">{{ asset.stockName }}</el-descriptions-item>
        <!--        <el-descriptions-item label="开盘价（￥）">{{ stock.openPrice }}</el-descriptions-item>-->
        <el-descriptions-item label="最新价格（￥）">
          <el-tag>{{ asset.stockPrice }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="持有数量">{{ asset.number }}</el-descriptions-item>
      </el-descriptions>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="卖出价格（￥）" prop="price" v-show="false">
          <el-input v-model="form.price" placeholder="请输入卖出价格" type="number"/>
        </el-form-item>
        <el-form-item label="卖出数量" label-width="120px" prop="number" v-show="false">
          <el-input v-model="form.number" placeholder="请输入卖出数量" type="number"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {getAsset, listAsset, sellAsset,sellForRepo,listForRepo} from "@/api/invest/asset/asset";
  import {listFinishStock, getStock, delStock, addStock, updateStock, exportStock,selectUpdateStockList} from "@/api/operate/stock";

  export default {
    name: "Asset",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 资产表格数据
        assetList: [],
        // 人才股票表格数据
        stockList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          stockId: null,
          holderId: null,
          number: null,
          deptId: null,
        },

        //卖出弹窗
        asset: "",
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询资产列表 */
      getList() {
        this.loading = true;
        listForRepo(this.queryParams).then(response => {
          this.assetList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          assetId: null,
          price: null,
          number: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加资产";
      },
      /** 卖出按钮操作 */
      handleUpdate(row) {
        const id = row.id || this.ids
        getAsset(id).then(response => {
          console.log(response.data)
          this.asset = response.data
          this.form.assetId = response.data.id;
          this.form.number = response.data.number;
          this.$set(this.form, "price", response.data.stockPrice);
          this.open = true;
          this.title = "卖出";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            sellForRepo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        });
      },
    }
  };
</script>
